package com.sk.weichat.ui.contacts;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sk.weichat.AppConstant;
import com.sk.weichat.R;
import com.sk.weichat.bean.message.GroupMessage;
import com.sk.weichat.db.dao.GroupMessageDao;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.groupchat.ApplyForAddGroupActivity;
import com.sk.weichat.ui.groupchat.OpenGroupInfoActivity;
import com.sk.weichat.ui.me.PersonalCenterActivity;
import com.sk.weichat.ui.message.MucChatActivity;
import com.sk.weichat.ui.message.multi.RoomInfoActivity;
import com.sk.weichat.util.TimeUtils;
import com.sk.weichat.util.ToastUtil;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * 群管理员设置界面
 * create by swy 2019-09-21
 */
public class GroupManagerSetActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mImgAvatar;
    private TextView mTvName;
    private TextView mTvContent;
    private TextView mTvTime;
    private GroupMessage mGroupMessage;
    private TextView mTvHandler;
    private TextView mTvState;
    private LinearLayout mLlHandler;
    private TextView mTvTitle;
    private String packetId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_message_layout);
        packetId = getIntent().getStringExtra("VERIFY_MESSAGE_PACKET");
        mGroupMessage = GroupMessageDao.getInstance().getGroupMessage(coreManager.getSelf().getUserId(), packetId);
        initActionBar();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    private void initActionBar() {
        getSupportActionBar().hide();

        mTvTitle = findViewById(R.id.tv_title_center);
        mTvTitle.setText("设置管理员");

        findViewById(R.id.iv_title_left).setOnClickListener(this);
    }

    private void initView() {
        mImgAvatar = findViewById(R.id.img_group_message_avatar);
        mTvName = findViewById(R.id.tv_group_message_name);
        mTvContent = findViewById(R.id.tv_group_message_content);
        mTvTime = findViewById(R.id.tv_group_message_time);
        mTvHandler = findViewById(R.id.tv_group_message_handler);
        mTvState = findViewById(R.id.tv_group_message_state);
        mLlHandler = findViewById(R.id.ll_group_message_handler);

        mTvTime.setText(TimeUtils.getFriendlyTimeDesc(this, mGroupMessage.getTimeSend()));

        //再次申请
        mTvState.setOnClickListener(this);
        mImgAvatar.setOnClickListener(this);
        mTvContent.setOnClickListener(this);
        mLlHandler.setOnClickListener(this);


        String content = "";
        switch (mGroupMessage.getState()) {
            case GroupMessage.STATUS_GM_10:
                //设置管理员
                mTvTitle.setText("设置管理员");
                mTvName.setText(mGroupMessage.getRoomName());
                if (mGroupMessage.getOwnerId().equals(mGroupMessage.getToUserId())) {
                    content = "将你设置为管理员";
                } else {
                    content = "将 <font color=#27C4FF>" + mGroupMessage.getToUserName() + "<font/> 设置为管理员";

                }
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgAvatar);
                mTvHandler.setText(mGroupMessage.getHandler());
                break;

            case GroupMessage.STATUS_GM_11:
                //取消管理员
                mTvTitle.setText("取消管理员");
                mTvName.setText(mGroupMessage.getRoomName());
                if (mGroupMessage.getOwnerId().equals(mGroupMessage.getToUserId())) {
                    content = "已取消你的管理员身份";
                } else {
                    content = "已取消 <font color=#27C4FF>" + mGroupMessage.getToUserName() + "<font/> 的管理员身份";
                }
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgAvatar);
                mTvHandler.setText(mGroupMessage.getHandler());
                break;

            case GroupMessage.STATUS_GM_12:
                //你退出群
                mTvTitle.setText("退出群");
                mTvName.setText(mGroupMessage.getRoomName());
                content = "你退出群 <font color=#27C4FF>" + mGroupMessage.getRoomName() + "<font/>";
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgAvatar);
                mLlHandler.setVisibility(View.GONE);
                break;

            case GroupMessage.STATUS_GM_13:
                //你被移出群
                mTvTitle.setText("移出群");
                mTvName.setText(mGroupMessage.getRoomName());
                content = "已将你移出群";
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgAvatar);
                mTvHandler.setText(mGroupMessage.getHandler());
                mTvState.setVisibility(View.VISIBLE);
                break;

            case GroupMessage.STATUS_GM_14:
                //其他人退出群
                mTvTitle.setText("退出群");
                mTvName.setText(mGroupMessage.getToUserName());
                content = "已退出群 <font color=#27C4FF>" + mGroupMessage.getRoomName() + "<font/>";
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getToUserId(), mImgAvatar, true);
                mLlHandler.setVisibility(View.GONE);
                break;
            case GroupMessage.STATUS_GM_15:
                //其他人被移出群
                mTvTitle.setText("移出群");
                mTvName.setText(mGroupMessage.getRoomName());
                content = "已将 <font color=#27C4FF>" + mGroupMessage.getToUserName() + "<font/> 移出群";
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgAvatar);
                mTvHandler.setText(mGroupMessage.getHandler());
                break;
            case GroupMessage.STATUS_GM_18:
                //转让群主给你
                mTvTitle.setText("转让群");
                mTvName.setText(mGroupMessage.getRoomName());
                content = "已转让群给你";
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgAvatar);
                mTvHandler.setText(mGroupMessage.getHandler());
                break;
            case GroupMessage.STATUS_GM_19:
                //转让群主给他人
                mTvTitle.setText("转让群");
                mTvName.setText(mGroupMessage.getRoomName());
                content = "已转让群给 <font color=#27C4FF>" + mGroupMessage.getToUserName() + "<font/>";
                mTvContent.setText(Html.fromHtml(content));
                AvatarHelper.getInstance().displayAvatar(mGroupMessage.getOwnerId(), mGroupMessage, mImgAvatar);
                mTvHandler.setText("处理人：" + mGroupMessage.getHandler());
                break;

        }
    }

    /**
     * @author: czl
     * @description 申请加群
     * @date: 2019/9/18 9:28
     */
    private void roomAutoJoin() {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("roomId", mGroupMessage.getRoomId());
        DialogHelper.showDefaulteMessageProgressDialog(GroupManagerSetActivity.this);

        HttpUtils.post().url(coreManager.getConfig().ROOM_AUTO_JOIN)
                .params(params).build().execute(new BaseCallback<String>(String.class) {
            @Override
            public void onResponse(ObjectResult<String> result) {
                DialogHelper.dismissProgressDialog();
                if (result.getResultCode() == 1) {//加入成功 直接进群
                    EventBus.getDefault().post("cancle");//关闭页面
                    //进入群聊页面
                    Intent intent = new Intent(GroupManagerSetActivity.this, MucChatActivity.class);
                    intent.putExtra(AppConstant.EXTRA_USER_ID, mGroupMessage.getUserId());
                    intent.putExtra(AppConstant.EXTRA_NICK_NAME, mGroupMessage.getRoomName());
                    intent.putExtra(AppConstant.EXTRA_IS_GROUP_CHAT, true);
                    startActivity(intent);
                    finish();
                } else if (result.getResultCode() == 1040320) {//群主开启了加群验证  跳转到发送加群申请页面
                    startActivity(new Intent(GroupManagerSetActivity.this, ApplyForAddGroupActivity.class)
                            .putExtra("RoomId", mGroupMessage.getRoomId()));
                } else if (result.getResultCode() == 1040319){//已加入群 已在群内
                    ToastUtil.showToast(GroupManagerSetActivity.this, result.getResultMsg());
                    EventBus.getDefault().post("cancle");//关闭页面
                    //进入群聊页面
                    Intent intent = new Intent(GroupManagerSetActivity.this, MucChatActivity.class);
                    intent.putExtra(AppConstant.EXTRA_USER_ID, mGroupMessage.getUserId());
                    intent.putExtra(AppConstant.EXTRA_NICK_NAME, mGroupMessage.getRoomName());
                    intent.putExtra(AppConstant.EXTRA_IS_GROUP_CHAT, true);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUtil.showToast(GroupManagerSetActivity.this, result.getResultMsg());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                DialogHelper.dismissProgressDialog();
                ToastUtil.showErrorNet(GroupManagerSetActivity.this);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_group_message_state:
                roomAutoJoin();
                break;
            case R.id.img_group_message_avatar:
                //点击头像
                clickAvatar();
                break;
            case R.id.tv_group_message_content:
                clickContent();
                break;
            case R.id.ll_group_message_handler:
                clickHandler();
                break;

        }
    }

    private void clickHandler() {
        switch (mGroupMessage.getState()) {
            case GroupMessage.STATUS_GM_10://设置管理员
            case GroupMessage.STATUS_GM_11://取消管理员
            case GroupMessage.STATUS_GM_13://你被移出群
            case GroupMessage.STATUS_GM_15://其他人被踢出群
            case GroupMessage.STATUS_GM_18://转让群主给你
            case GroupMessage.STATUS_GM_19://转让群主给你他人
                Intent intent = new Intent(mContext, PersonalCenterActivity.class);
                intent.putExtra(AppConstant.EXTRA_USER_ID, mGroupMessage.getHandlerId());
                mContext.startActivity(intent);
                break;
        }
    }

    private void clickContent() {
        Intent intent;
        switch (mGroupMessage.getState()) {
            case GroupMessage.STATUS_GM_10:
            case GroupMessage.STATUS_GM_11:
            case GroupMessage.STATUS_GM_15:
            case GroupMessage.STATUS_GM_19:
                if(!mGroupMessage.getOwnerId().equals(mGroupMessage.getToUserId())){
                    intent = new Intent(mContext, PersonalCenterActivity.class);
                    intent.putExtra(AppConstant.EXTRA_USER_ID, mGroupMessage.getToUserId());
                    mContext.startActivity(intent);
                }
                break;

            case GroupMessage.STATUS_GM_12://你退出群
                startActivity(new Intent(mContext, OpenGroupInfoActivity.class)
                        .putExtra("RoomId", mGroupMessage.getRoomId())
                .putExtra("packetId",packetId));
                break;
            case GroupMessage.STATUS_GM_14://其他人退出群
                intent = new Intent(mContext, RoomInfoActivity.class);
                intent.putExtra(AppConstant.EXTRA_USER_ID, mGroupMessage.getUserId());
                intent.putExtra(AppConstant.EXTRA_IS_GROUP_CHAT, true);
                mContext.startActivity(intent);
                break;

        }
    }

    private void clickAvatar() {
        Intent intent;
        switch (mGroupMessage.getState()) {

            case GroupMessage.STATUS_GM_12://你退出群
            case GroupMessage.STATUS_GM_13://你被移出群
                startActivity(new Intent(mContext, OpenGroupInfoActivity.class)
                        .putExtra("RoomId", mGroupMessage.getRoomId())
                .putExtra("packetId",packetId));
                break;

            case GroupMessage.STATUS_GM_14://其他人退出群
                intent = new Intent(mContext, PersonalCenterActivity.class);
                intent.putExtra(AppConstant.EXTRA_USER_ID, mGroupMessage.getToUserId());
                mContext.startActivity(intent);
                break;

            default:
                intent = new Intent(mContext, RoomInfoActivity.class);
                intent.putExtra(AppConstant.EXTRA_USER_ID, mGroupMessage.getUserId());
                intent.putExtra(AppConstant.EXTRA_IS_GROUP_CHAT, true);
                mContext.startActivity(intent);

        }
    }
}
